﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace QuanLySimSoDep
{
    public partial class Form1 : Form
    {
        private SqlConnection m_sqlConnection;
        private string m_strChuoiKetNoi;
        private string m_strChuoiSelect;
        private string m_strChuoiInsert;
        private SqlDataAdapter m_sqlDataAdapter;
        private DataSet m_dDataSet;
        private DataTable m_DataTable;
        //chuoi thuc thi
        private SqlCommand m_sqlCommandSelect;
        private SqlCommand m_sqlCommandInsert;
        private int iTamCo;
        private int iTuQui;
        private int dem;
        public Form1()
        {
            InitializeComponent();
            m_strChuoiKetNoi = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";
            m_strChuoiSelect = "select * from NhapSim";
            m_sqlConnection = new SqlConnection(m_strChuoiKetNoi);
            m_sqlCommandSelect = new SqlCommand(m_strChuoiSelect, m_sqlConnection);
            m_sqlDataAdapter = new SqlDataAdapter(m_strChuoiSelect, m_sqlConnection);
            m_dDataSet = new DataSet();
            m_sqlDataAdapter.Fill(m_dDataSet, "NhapSim");
            
            m_DataTable = new DataTable();
            try
            {
                m_sqlConnection.Open();
            }
            catch (Exception ex)
            {

                MessageBox.Show("Loi Ket Noi" + ex.ToString());
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void cbxTimKiem_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            if (cbxTimKiem.SelectedItem == "Mạng")
            {
                pnNhaCungCap.Visible = true;
                pnLoai.Visible = false;
                pnKhac.Visible = false;
                cbxTamCo.Checked = false;
                cbxTuQui.Checked = false;
                cbxCaHai.Checked = false;
                cbx11So.Checked = false;
                cbx10So.Checked = false;
            }
            if (cbxTimKiem.SelectedItem == "Loại Sim")
            {
                pnNhaCungCap.Visible = false;
                pnLoai.Visible = true;
                pnKhac.Visible = false;
                cbxVietNam.Checked = false;
                cbxViettel.Checked = false;
                cbxVina.Checked = false;
                cbxMobi.Checked = false;
                cbxTamCo.Checked = false;
                cbxTuQui.Checked = false;
            }
            if (cbxTimKiem.SelectedItem == "Khác")
            {
                pnKhac.Visible = true;
                pnLoai.Visible = false;
                pnNhaCungCap.Visible = false;
                cbxVietNam.Checked = false;
                cbxViettel.Checked = false;
                cbxVina.Checked = false;
                cbxMobi.Checked = false;
                cbxCaHai.Checked = false;
                cbx11So.Checked = false;
                cbx10So.Checked = false;
            }
        }

        private void cbxCaHai_CheckedChanged(object sender, EventArgs e)
        {
            //if (cbxCaHai.Checked == true)
            {
                cbx10So.Checked = false;
                cbx11So.Checked = false;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadDuLieu();  
        }
        public void LoadDuLieu()
        {
            try
            {
                m_sqlDataAdapter.SelectCommand = m_sqlCommandSelect;
                //m_dDataSet.Reset();
                m_sqlDataAdapter.Fill(m_dDataSet);
                m_DataTable = m_dDataSet.Tables[0];
                dataGridView1.DataSource = m_DataTable.DefaultView;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Co Loi Trong Qua TrinH Ket Noi:" + ex.ToString());
            }

        }
        public void KiemTra()
        {
            try
            {
                int a = int.Parse(txtSoSim.Text);
                if (txtSoSim.Text.Length < 10 || txtSoSim.Text.Length > 11)
                {
                    chkSoSim.Text = "Phải là 10 or 11 số";
                }
                else
                    chkSoSim.Text = "<< OK";
            }
            catch (Exception e)
            {
                chkSoSim.Text = "Sim phải là số";
            }
        }
        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            int iRowIndex = -1;
            if (dataGridView1.SelectedCells.Count > 0)
            {
                iRowIndex = this.dataGridView1.SelectedCells[0].RowIndex;
            }
            if (iRowIndex >= 0)
            {
                
                DataGridViewRow dtRow=this.dataGridView1.Rows[iRowIndex];
                if(dtRow.Cells[0].Value!=null)
                {
                    txtMaSim.Text   = dtRow.Cells[0].Value.ToString();
                    txtSoSim.Text   = dtRow.Cells[2].Value.ToString();
                    txtMang.Text    = dtRow.Cells[3].Value.ToString();
                    txtGia.Text     = dtRow.Cells[4].Value.ToString();
                    txtLoaiSim.Text = dtRow.Cells[5].Value.ToString();
                    txtDauSo.Text   = dtRow.Cells[6].Value.ToString();
                }
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
        private void btnNhap_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtGia.Text != "" && txtSoSim.Text != "")
                {
                    //Ham kiem tra tam co tu qui
                    if (TachSim(txtSoSim.ToString()) == 1)
                    {
                        iTamCo = 1;
                    }
                    else
                    {
                        iTamCo = 0;
                    }
                    if (TachSim(txtSoSim.ToString()) == 2)
                    {
                        iTuQui = 1;
                    }
                    else
                    {
                        iTuQui = 0;
                    }
                    //string strMaSo = this.txtMaSim.Text;
                    //loai sim
                    string strDemSo = this.txtSoSim.Text;
                    int iLoaiSim = strDemSo.Length;
                    //ma mang
                    string strMang = "X";
                    string strMaMang = "X";
                    string strDauSo = "X";

                    //
                    int tempTach = txtSoSim.Text.Length;
                    String[] x = new String[tempTach];
                    int dem = 0;
                    for (int k = 0; k < tempTach; k++)
                        x[k] = txtSoSim.Text.Substring(k, 1);
                    //
                    // detect mạng
                    string[] tamViettel = { "098", "097", "0165", "0166", "0167", "0168", "0169" };
                    string[] tamMobifone = { "090", "093", "0122", "0126", "0121", "0128", "0120" };
                    for (int i = 0; i < 7; i++)
                    {
                        if (x[0] + x[1] + x[2] == tamViettel[i] || x[0] + x[1] + x[2] + x[3] == tamViettel[i])
                        {
                            strDauSo = tamViettel[i];
                            strMaMang = "V";
                            strMang = "Viettel";
                        }
                        if (x[0] + x[1] + x[2] == tamMobifone[i] || x[0] + x[1] + x[2] + x[3] == tamMobifone[i])
                        {
                            strDauSo = tamMobifone[i];
                            strMaMang = "M";
                            strMang = "Mobifone";
                        }

                    }
                    //
                    //
                    string strSoSim = this.txtSoSim.Text;
                    string strGia = this.txtGia.Text;
                    string strHang = "Chưa Bán";
                    string temp;

                    DataRow dtRow = m_DataTable.NewRow();
                    //dtRow["MaSim"] = this.txtMaSim.Text;
                    dtRow["MaMang"] = strMaMang;
                    dtRow["Số Sim"] = this.txtSoSim.Text;
                    dtRow["Mang"] = strMang;
                    dtRow["Gia"] = this.txtGia.Text;
                    dtRow["LoaiSim"] = txtSoSim.Text.Length;
                    dtRow["DauSo"] = strDauSo;
                    dtRow["Hàng"] = strHang;
                    dtRow["TamCo"] = iTamCo;
                    dtRow["TuQui"] = iTuQui;
                    m_DataTable.Rows.Add(dtRow);

                    //
                    m_strChuoiInsert = "insert into NhapSim values('" + strMaMang + "','" + strSoSim + "','" + strMang + "','" + strGia + "','" + iLoaiSim + "','" + strDauSo + "','" + strHang + "','" + iTamCo + "','" + iTuQui + "')";
                }
                m_sqlCommandInsert = new SqlCommand(m_strChuoiInsert, m_sqlConnection);
                m_sqlDataAdapter.InsertCommand = m_sqlCommandInsert;
                m_sqlDataAdapter.Update(m_dDataSet);
                Application.DoEvents();
                //m_dDataSet.AcceptChanges();
                LoadDuLieu();

            }
            catch (Exception ex)
            {

                MessageBox.Show("Co Loi Trong Qua TrinH Ket Noi:" + ex.ToString());
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (this.m_sqlConnection.State == ConnectionState.Open)
            {
                this.m_sqlConnection.Close();
            }
        }
         public void kiemtra(string s1,string s2)
         {
            int i,j=0;
            for(i=0;s2!="null";i++)
            if(s2==s1) j++;
            else
            {
               i-=j;
               j=0;
            }
            if(s2=="null") 
            {
                LoadDuLieu();  
            }
            else
            {
                MessageBox.Show("Du Lieu Ko Ton Tai");
            }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            m_dDataSet.Reset();
            if (cbxViettel.Checked == true)
            {
                m_strChuoiSelect = "select * from NhapSim where Mang ='Viettel'";
                m_sqlCommandSelect=new SqlCommand(m_strChuoiSelect,m_sqlConnection);
                LoadDuLieu();
            }
            if (cbxVina.Checked == true)
            {
                m_strChuoiSelect = "select * from NhapSim where Mang = 'Vinafore'";
                m_sqlCommandSelect = new SqlCommand(m_strChuoiSelect, m_sqlConnection);
                LoadDuLieu();
            }
            if (cbxMobi.Checked == true)
            {
                m_strChuoiKetNoi = "SELECT * FROM NhapSim where Mang='Mobifone'";
                m_sqlCommandSelect = new SqlCommand(m_strChuoiKetNoi, m_sqlConnection);
                LoadDuLieu();
            }
            if (cbxVietNam.Checked == true)
            {
                m_strChuoiKetNoi = "SELECT * FROM NhapSim where Mang='Vietnammobile'";
                m_sqlCommandSelect = new SqlCommand(m_strChuoiKetNoi, m_sqlConnection);
                LoadDuLieu();
            }
            if (cbxTamCo.Checked == true)
            {
                m_strChuoiKetNoi = "SELECT * FROM NhapSim where TamCo='0'";
                m_sqlCommandSelect = new SqlCommand(m_strChuoiKetNoi, m_sqlConnection);
                LoadDuLieu();
            }
            if (cbx10So.Checked == true)
            {
                 m_strChuoiKetNoi = "SELECT * FROM NhapSim where LEN([Số Sim])='10'";
                 m_sqlCommandSelect = new SqlCommand(m_strChuoiKetNoi, m_sqlConnection);
                 LoadDuLieu();
            }
            if (cbx11So.Checked == true)
            {
                m_strChuoiKetNoi = "SELECT * FROM NhapSim where LEN([Số Sim])='11'";
                m_sqlCommandSelect = new SqlCommand(m_strChuoiKetNoi, m_sqlConnection);
                LoadDuLieu();
            }
            if (cbxCaHai.Checked == true)
            {
                m_strChuoiKetNoi = "SELECT * FROM NhapSim where LEN([Số Sim])='10' or LEN([Số Sim])='11'";
                m_sqlCommandSelect = new SqlCommand(m_strChuoiKetNoi, m_sqlConnection);
                LoadDuLieu();
            }
            if (cbxTuQui.Checked == true)
            {
                m_strChuoiKetNoi = "SELECT * FROM NhapSim where TuQui=1";
                m_sqlCommandSelect = new SqlCommand(m_strChuoiKetNoi, m_sqlConnection);
                LoadDuLieu();
            }
            if (cbxTamCo.Checked == true)
            {
                m_strChuoiKetNoi = "SELECT * FROM NhapSim where TamCo=1 and TuQui=0";
                m_sqlCommandSelect = new SqlCommand(m_strChuoiKetNoi, m_sqlConnection);
                LoadDuLieu();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            txtMaSim.Text = "";
            txtSoSim.Text = "";
            txtMang.Text = "";
            txtGia.Text = "";
            txtLoaiSim.Text = "";
            txtDauSo.Text = "";
        }
        public int TachSim(string strChuoi)
        {
            int tempTach = strChuoi.Length;
            String[] x = new String[tempTach];
            //int dem = 0;
            for (int k = 0; k < tempTach; k++)
                x[k] = strChuoi.Substring(k, 1);
            for (int i = 0; i < tempTach - 1; i++)
            {
                {
                    if (x[i] == x[i + 1] && x[i] == x[i + 2])
                        dem++;
                }
            }
            return dem;
        }
        private void btnKiemTra_Click(object sender, EventArgs e)
        {
            KiemTra();
        }
    }
}
